/* 
   Copyright 2012 Ryne Okimoto

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package delegate;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import recipe.RecipeData;

import manager.FileManager;

/**
 * Class - EditRecipeViewController
 * Description - <DESCRIPTION>
 * Notes - <NOTES>
 * 
 * @author Ryne Okimoto
 */

@SuppressWarnings("serial")
public class EditRecipeViewController extends JFrame{
	private JButton          btnDone        = new JButton("Done");
	private JButton          btnCancel      = new JButton("Cancel");
	private JTextField       txtRecipeName  = new JTextField(30);
	private JTextField       txtCategory    = new JTextField(15);
	private JTextField       txtTags        = new JTextField(30);
	private JTextArea        txaIngredients = new JTextArea(10, 30);
	private JTextArea        txaInstructions = new JTextArea(10, 30);
	
	public EditRecipeViewController(FileManager recipeManager){
		RecipeData recipe = recipeManager.getData();
		JPanel view = new JPanel();
		
		// Set layout and properties
		view.setLayout(new GridBagLayout());
		this.setResizable(false);
		
		// Set focus
		btnDone.setFocusable(true);
		btnCancel.setFocusable(true);
		txtRecipeName.setFocusable(true);
		txtCategory.setFocusable(true);
		txtTags.setFocusable(true);
		txaIngredients.setFocusable(true);
		txaInstructions.setFocusable(true);
		
		// Set text
		txtRecipeName.setText(recipe.getRecipeName());
		txtCategory.setText(recipe.getCategory());
		txtTags.setText(recipe.getTags());
		txaIngredients.setText(recipe.getIngredients());
		txaInstructions.setText(recipe.getInstructions());
		
		// Set constraints
		GridBagConstraints gbcTxaInstructions = new GridBagConstraints();
		GridBagConstraints gbcTxtRecipeName   = new GridBagConstraints();
		GridBagConstraints gbcBtnDone         = new GridBagConstraints();
		GridBagConstraints gbcTxtCategory     = new GridBagConstraints();
		GridBagConstraints gbcTxaIngredients  = new GridBagConstraints();
		GridBagConstraints gbcTxtTags         = new GridBagConstraints();
		GridBagConstraints gbcBtnCancel       = new GridBagConstraints();
		
		// Constraints - instructions
		gbcTxaInstructions.gridx = 0;
		gbcTxaInstructions.gridy = 4;
		gbcTxaInstructions.fill = GridBagConstraints.HORIZONTAL;
		gbcTxaInstructions.insets = new Insets(10, 10, 10, 10);
		gbcTxaInstructions.gridwidth = 3;
		
		// Constraints - recipe name
		gbcTxtRecipeName.gridx = 0;
		gbcTxtRecipeName.gridy = 0;
		gbcTxtRecipeName.insets = new Insets(10, 10, 0, 10);
		
		// Constraints - category
		gbcTxtCategory.gridx = 0;
		gbcTxtCategory.gridy = 1;
		gbcTxtCategory.insets = new Insets(10, 10, 0, 10);
		
		// Constraints - tags
		gbcTxtTags.gridx = 0;
		gbcTxtTags.gridy = 2;
		gbcTxtTags.insets = new Insets(10, 10, 0, 10);
		
		// Constraints - ingredients
		gbcTxaIngredients.gridx = 0;
		gbcTxaIngredients.gridy = 3;
		gbcTxaIngredients.fill = GridBagConstraints.HORIZONTAL;
		gbcTxaIngredients.insets = new Insets(10, 10, 0, 10);
		gbcTxaIngredients.gridwidth = 3;
		
		// Constraints - done button
		gbcBtnDone.gridx = 0;
		gbcBtnDone.gridy = 5;
		gbcBtnDone.insets = new Insets(0, 10, 0, 10);
		
		// Constraints - cancel button
		gbcBtnCancel.gridx = 0;
		gbcBtnCancel.gridy = 6;
		gbcBtnDone.insets = new Insets(10, 10, 10, 10);
		
		// Add components
		view.add(txtRecipeName, gbcTxtRecipeName);
		view.add(txtCategory, gbcTxtCategory);
		view.add(txaInstructions, gbcTxaInstructions);
		view.add(txaIngredients, gbcTxaIngredients);
		view.add(btnDone, gbcBtnDone);
		view.add(txtTags, gbcTxtTags);
		view.add(btnCancel, gbcBtnCancel);
		
		// Register listeners
		btnCancel.addActionListener(new CancelClickedListener());
		
		
		// Finish view
		this.setContentPane(view);
		this.setTitle("Edit: " + recipe.getRecipeName());
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.pack();
		
	} // constructor
	
	/**
	 * Class - CancelClickedListener
	 * Description - 
	 *
	 */
	private class CancelClickedListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			EditRecipeViewController.this.dispose();
		}
	}
	
	
	
	public static void main (String[] args){
		
		EditRecipeViewController vc = new EditRecipeViewController(new FileManager("testRead.rcp"));
		vc.setVisible(true);
	} // main

}
